<template>
  <div class="menu-create">
    <el-card class="box-card">
      <template
        #header
        class="clearfix"
      >
        <span>{{ isEdit ? '添加广告' : '添加菜单' }}</span>
      </template>
      <el-form
        ref="formDom"
        :model="form"
        label-width="80px"
      >
        <el-form-item
          prop="name"
          label="广告名称"
        >
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item
          prop="spaceId"
          label="广告位置"
        >
          <el-select
            v-model="form.spaceId"
            placeholder="请选择广告位置"
          >
            <el-option
              v-for="item in advSpaces"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="startTimeFormatString"
          label="开始时间"
        >
          <el-date-picker
            v-model="form.startTimeFormatString"
            type="datetime"
            placeholder="选择开始时间"
          />
        </el-form-item>
        <el-form-item
          prop="endTimeFormatString"
          label="到期时间"
        >
          <el-date-picker
            v-model="form.endTimeFormatString"
            type="datetime"
            placeholder="选择到期时间"
          />
        </el-form-item>
        <el-form-item
          prop="status"
          label="上线/下线"
        >
          <el-radio-group v-model="form.status">
            <el-radio :label="1">
              上线
            </el-radio>
            <el-radio :label="0">
              下线
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          prop="img"
          label="上线/下线"
        >
          <uploadImage
            v-model="form.img"
            :limit="5"
          />
        </el-form-item>

        <el-form-item
          prop="link"
          label="广告链接"
        >
          <el-input v-model="form.link" />
        </el-form-item>

        <el-form-item
          prop="text"
          label="备注"
        >
          <el-input v-model="form.text" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit(form)"
          >
            提交
          </el-button>
          <el-button
            v-if="!isEdit"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { defineComponent, onBeforeMount, Ref, ref } from 'vue'
import { getAllSpaces, saveOrUpdate } from '/@/services/advert'
import { ElMessage } from 'element-plus'
import uploadImage from '/@/views/course/components/CourseImage.vue'

interface Form {
  endTimeFormatString: Date | null
  img: string
  link: string
  name: string
  spaceId: number | null
  startTimeFormatString: Date | null
  status: number | null
  text: string
}
let form: Ref<Form>
// 上传数据
// 提交
const useAddOrEditMenu = () => {
  const router = useRouter()
  const onSubmit = async (form: Form) => {
    console.log(form)
    // return
    // const { data } = await createOrUpdateMenu(form)
    // if (data.code === '000000') {
    //   ElMessage.success('添加菜单成功!')
    //   router.push('/menu')
    // }
  }
  return {
    onSubmit
  }
}
// 初始化数据
const uesInit = () => {
  const route = useRoute()
  const routeId = +route.params?.id
  const isEdit = route.name === 'menuEdit'
  const advSpaces = ref([{ id: 1, name: '无选项' }])
  const getSpacesList = async () => {
    try {
      const { data } = await getAllSpaces()
      if (data.success) {
        advSpaces.value = data?.content || []
      }
    } catch (error) {
      ElMessage.error('获取上级菜单失败!')
    }
  }
  onBeforeMount(async () => {
    // 获取上级菜单
    getSpacesList()
    if (isEdit) {
      // const { data } = await getMenuInfo(routeId)
      // form.value = data.data
    }
  })
  return {
    routeId,
    isEdit,
    advSpaces
  }
}
export default defineComponent({
  name: 'MenuIndex',
  components: {
    uploadImage
  },
  setup () {
    form = ref<Form>({
      endTimeFormatString: null,
      img: '',
      link: '',
      name: '',
      spaceId: null,
      startTimeFormatString: null,
      status: null,
      text: ''
    })
    return {
      ...uesInit(),
      ...useAddOrEditMenu(),
      form,
      formDom: null
    }
  }
})
</script>

<style lang="scss" scoped>
  .box-card {
    :deep(.el-card) {
      height: calc(100% - 60px - 45px);
    }
  }
</style>
